﻿namespace HySoft.Cash.DB
{
    using Huayou.Common.Database;
    using Huayou.Utils;
    using HySoft.Cash.BaseClass;
    using HySoft.Cash.Structure;
    using System;
    using System.Data;

    public class TorginfoDB : BaseDB
    {
        public TorginfoDB(DBOperate oper)
            : base(oper)
        {
        }

        public bool Add(TorginfoDT detail)
        {
            int num = 0;
            string strSql = string.Concat(new object[] { 
                "INSERT INTO tOrgInfo (AgreementDate,OrgName,TradingName,ABN,Industry,OfficeAddress,WebSite,ContactPerson,sPosition,ExpCreditLimit,PostalAddress,ContactPhone1,ContactPhone2,Fax,Mobile,PreferredEmail,AltEmail1,AltEmail2,UserName,PassWord,RegDate,IsValid,CreditLimit,FeeBalance,CreditBalance) VALUES ('", detail.AgreementDate, "','", StringUtils.ToSQL(detail.OrgName), "','", StringUtils.ToSQL(detail.TradingName), "','", StringUtils.ToSQL(detail.ABN), "','", StringUtils.ToSQL(detail.Industry), "','", StringUtils.ToSQL(detail.OfficeAddress), "','", StringUtils.ToSQL(detail.WebSite), "','", StringUtils.ToSQL(detail.ContactPerson), 
                "','", StringUtils.ToSQL(detail.sPosition), "',", detail.ExpCreditLimit, ",'", StringUtils.ToSQL(detail.PostalAddress), "','", StringUtils.ToSQL(detail.ContactPhone1), "','", StringUtils.ToSQL(detail.ContactPhone2), "','", StringUtils.ToSQL(detail.Fax), "','", StringUtils.ToSQL(detail.Mobile), "','", StringUtils.ToSQL(detail.PreferredEmail), 
                "','", StringUtils.ToSQL(detail.AltEmail1), "','", StringUtils.ToSQL(detail.AltEmail2), "','", StringUtils.ToSQL(detail.UserName), "','", StringUtils.ToSQL(detail.PassWord), "','", detail.RegDate, "',", (int)detail.IsValid, ",", detail.CreditLimit, ",", detail.FeeBalance, 
                ",", detail.CreditBalance, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public int AddId(TorginfoDT detail)
        {
            int num = 0;
            int num2 = 0;
            string strSql = string.Concat(new object[] { 
                "INSERT INTO tOrgInfo (AgreementDate,OrgName,TradingName,ABN,Industry,OfficeAddress,WebSite,ContactPerson,sPosition,ExpCreditLimit,PostalAddress,ContactPhone1,ContactPhone2,Fax,Mobile,PreferredEmail,AltEmail1,AltEmail2,UserName,PassWord,RegDate,IsValid,CreditLimit,FeeBalance,CreditBalance) VALUES ('", detail.AgreementDate, "','", StringUtils.ToSQL(detail.OrgName), "','", StringUtils.ToSQL(detail.TradingName), "','", StringUtils.ToSQL(detail.ABN), "','", StringUtils.ToSQL(detail.Industry), "','", StringUtils.ToSQL(detail.OfficeAddress), "','", StringUtils.ToSQL(detail.WebSite), "','", StringUtils.ToSQL(detail.ContactPerson), 
                "','", StringUtils.ToSQL(detail.sPosition), "',", detail.ExpCreditLimit, ",'", StringUtils.ToSQL(detail.PostalAddress), "','", StringUtils.ToSQL(detail.ContactPhone1), "','", StringUtils.ToSQL(detail.ContactPhone2), "','", StringUtils.ToSQL(detail.Fax), "','", StringUtils.ToSQL(detail.Mobile), "','", StringUtils.ToSQL(detail.PreferredEmail), 
                "','", StringUtils.ToSQL(detail.AltEmail1), "','", StringUtils.ToSQL(detail.AltEmail2), "','", StringUtils.ToSQL(detail.UserName), "','", StringUtils.ToSQL(detail.PassWord), "','", detail.RegDate, "',", (int)detail.IsValid, ",", detail.CreditLimit, ",", detail.FeeBalance, 
                ",", detail.CreditBalance, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                    if (num > 0)
                    {
                        strSql = "SELECT @@IDENTITY AS 'Identity'";
                        DataTable table = base.ExecuteForDataTable(strSql);
                        if (table.Rows.Count > 0)
                        {
                            num2 = Convert.ToInt32(table.Rows[0][0].ToString());
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            if (num == 1)
            {
                return num2;
            }
            return 0;
        }

        public bool Delete(int Sid)
        {
            int num = 0;
            string strSql = "DELETE FROM tOrgInfo where (Sid=" + Sid + ")";
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public bool Edit(TorginfoDT detail)
        {
            int num = 0;
            string strSql = string.Concat(new object[] { 
                "UPDATE tOrgInfo set AgreementDate='", detail.AgreementDate, "',OrgName='", StringUtils.ToSQL(detail.OrgName), "',TradingName='", StringUtils.ToSQL(detail.TradingName), "',ABN='", StringUtils.ToSQL(detail.ABN), "',Industry='", StringUtils.ToSQL(detail.Industry), "',OfficeAddress='", StringUtils.ToSQL(detail.OfficeAddress), "',WebSite='", StringUtils.ToSQL(detail.WebSite), "',ContactPerson='", StringUtils.ToSQL(detail.ContactPerson), 
                "',sPosition='", StringUtils.ToSQL(detail.sPosition), "',ExpCreditLimit=", detail.ExpCreditLimit, ",PostalAddress='", StringUtils.ToSQL(detail.PostalAddress), "',ContactPhone1='", StringUtils.ToSQL(detail.ContactPhone1), "',ContactPhone2='", StringUtils.ToSQL(detail.ContactPhone2), "',Fax='", StringUtils.ToSQL(detail.Fax), "',Mobile='", StringUtils.ToSQL(detail.Mobile), "',PreferredEmail='", StringUtils.ToSQL(detail.PreferredEmail), 
                "',AltEmail1='", StringUtils.ToSQL(detail.AltEmail1), "',AltEmail2='", StringUtils.ToSQL(detail.AltEmail2), "',UserName='", StringUtils.ToSQL(detail.UserName), "',PassWord='", StringUtils.ToSQL(detail.PassWord), "',RegDate='", detail.RegDate, "',IsValid=", (int)detail.IsValid, ",CreditLimit=", detail.CreditLimit, ",FeeBalance=", detail.FeeBalance, 
                ",CreditBalance=", detail.CreditBalance, " where (Sid=", detail.Sid, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        /// <summary>
        /// 修正用户还款的余额
        /// </summary>
        /// <param name="remainBalanceAdded">新增加的余额</param>
        /// <returns></returns>
        public bool ModifyRemainBalance(int orgID, decimal remainBalanceAdded)
        {
            int num = 0;
            string strSql = string.Format("UPDATE tOrgInfo set [RemainBalance]= [RemainBalance]+{1} where Sid= {0}", orgID, remainBalanceAdded);
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public TorginfoDT Get(int Sid)
        {
            TorginfoDT odt = null;

            try
            {
                string strSql = string.Format("select {0} from tOrgInfo where [Sid]= {1}", TorginfoDT.allFields, Sid);
                DataTable table = base.ExecuteForDataTable(strSql);
                DataRow row = null;
                if (table.Rows.Count >= 0)
                {
                    row = table.Rows[0];
                    odt = TorginfoDT.ConvertFrom(row);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }

            return odt;
        }

        public DataTable GetList()
        {
            DataTable table = null;
            try
            {
                try
                {
                    string strSql = "select * from tOrgInfo";
                    table = base.ExecuteForDataTable(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return table;
        }
    }
}

